home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr18 / chessclk.zip / CHESSCLK.CPP next >
C/C++ Source or Header  |  1995-04-15  |  8KB  |  310 lines

  1. /****************************** Chess clock program ************************
  2. Full-featured chess clock: Digital displays of time
  3.                Move counter
  4.                White and Black clocks separately settable
  5.                Times from 1 minute to 99 hours
  6.                Low-time warning (optional)
  7.                Flashing "tick" indicator (optional)
  8. ****************************************************************************/
  9. // M\Cooper, 3425 Chessnut Ridge Rd., Grantsville, MD 21536-9801
  10.  
  11.  
  12. #include <stdio.h>
  13. #include <time.h>
  14. #include <conio.h>
  15. #include <stdlib.h>
  16. #include <graphics.h>
  17. #include <dos.h>
  18. #include "oscr.hpp"
  19. #include "chessclk.hpp"
  20.  
  21. #define XXPOS 60
  22. #define YYPOS 37
  23. //Base coordinates for time display
  24.  
  25. #define XWIDTH 580
  26. #define YHEIGHT 25
  27. #define XXMARGIN 50
  28. #define LXMARGIN 26
  29. //Whose_clock_is_running flag coordinates
  30.  
  31. void main()
  32. {
  33.    randomize();
  34.    opening_screen();
  35.    play();
  36. }
  37.  
  38. void CountdownTimer::clock_on()
  39. {
  40.    time_t prev_sec;
  41.    int ch;
  42.  
  43.       if( p == WHITE_ )
  44.      text_color = LIGHTBLUE;
  45.       else
  46.      text_color = DARKGRAY;
  47.  
  48.       setcolor( text_color );
  49.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 5 );
  50.       settextjustify( LEFT_TEXT, TOP_TEXT );
  51.  
  52.      ShowFlag();
  53.  
  54.       startn_t = time ( NULL );  //Click on stopwatch.
  55.       gotoxy( XXPOS * p + 1, YYPOS );
  56.       outtextxy( NAME_POS * p, 100, player [p] );
  57.       display_time();  //Otherwise initial time not displayed...
  58.  
  59.       while( !( ch = kbhit() ) )
  60.      {
  61.      prev_sec = seconds;
  62.      if( running_flag) //Is this 2nd or later lap?
  63.         interval_t = time( NULL ) - startn_t;
  64.      else
  65.         interval_t = time( NULL ) - start_t;
  66.      running_t = total_seconds - interval_t;
  67.      convert( running_t );
  68.  
  69.      if( seconds - prev_sec )
  70.         {
  71.         display_time();
  72.  
  73.         if( !seconds )
  74.            if( minutes == warning )
  75.           if( !hours )
  76.              if( time_warning_flag )
  77.             blatt();
  78.  
  79.         if( visual_ticking_flag ) // Show blinking box ticks?
  80.            {
  81.            setfillstyle( random ( PATTERNS ), random ( COLORS ) );
  82.            setcolor ( random ( COLORS ) );
  83.            setlinestyle( SOLID_LINE, 0xFFF, NORM_WIDTH );
  84.            bar( X_C - RADIUS, Y_C - RADIUS,
  85.             X_C + RADIUS, Y_C + RADIUS );
  86.            }
  87.         }
  88.  
  89.      if( timeout() )
  90.         exit_();
  91.      }
  92.  
  93.       ch = getch();
  94.       if( ch == ESC )
  95.      exit__();  // Quit.
  96.  
  97.       total_seconds = hours * 3600 + minutes * 60 + seconds;
  98.       running_flag = ON;  // Remember that clock was already running.
  99.  
  100.       return;
  101.  
  102. }
  103.  
  104. void CountdownTimer::display_moves()
  105. {
  106.    char buf[ 5 ];
  107.    static char ebuf[ 5 ];
  108.  
  109.       if( moves > 1 )
  110.      {
  111.      setcolor( WHITE );
  112.      settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  113.      settextjustify( CENTER_TEXT, TOP_TEXT );
  114.      outtextxy( MOVES_X, MOVES_Y, ebuf );
  115.      }
  116.  
  117.       sprintf( buf, "%003d", moves );
  118.       sprintf( ebuf, buf );
  119.       setcolor( LIGHTRED );
  120.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  121.       settextjustify( CENTER_TEXT, TOP_TEXT );
  122.       outtextxy( MOVES_X, MOVES_Y, buf );
  123.       
  124.       return;
  125. }
  126.  
  127. void graphics_setup( int background_color )
  128. {
  129.    int grdriver = VGA,
  130.        grmode = VGAHI;
  131.  
  132.        registerfarbgidriver( EGAVGA_driver_far );
  133.        registerfarbgifont( gothic_font_far );
  134.        registerfarbgifont( triplex_font_far );
  135.        initgraph( &grdriver, &grmode, "" );
  136.        setbkcolor( background_color );
  137.  
  138. }
  139.  
  140. void exit__()
  141. {
  142.       closegraph();
  143.       exit( QUIT );
  144. }
  145.  
  146.     /***************Routine to erase old numbers*************/
  147. void CountdownTimer::erase_numbers()
  148. {
  149.       setcolor ( WHITE ); 
  150.  
  151.       if( seconds == 59 )
  152.      outtextxy( p * BLK_TIME, Y_TIMEPOS, line_clear );
  153.       else
  154.      if( seconds == 9 || seconds == 19 || seconds == 29
  155.          || seconds == 39 || seconds == 49 )
  156.         outtextxy( p * BLK_TIME + POS1_OFFSET, Y_TIMEPOS,
  157.                line_clear + 6 );
  158.       else
  159.          outtextxy( p * BLK_TIME + POS_OFFSET, Y_TIMEPOS,
  160.             line_clear + 7 ); 
  161.  
  162.      return;
  163.  
  164. }
  165.  
  166.  
  167. void play()
  168. {
  169.    int hrs,
  170.        min;
  171.    char inputstr[ MAXLEN ],
  172.     inp;
  173.  
  174.       clrscr();
  175.  
  176.       textcolor ( LIGHTCYAN );
  177.       cprintf( "\n                              WHITE hours: " );
  178.       gets( inputstr );
  179.       hrs = atoi( inputstr );
  180.       cprintf( "                              WHITE minutes: " );
  181.       gets( inputstr );
  182.       min = atoi( inputstr );
  183.       CountdownTimer t1( hrs, min, WHITE_ );
  184.  
  185.       textcolor( RED );
  186.       cprintf( "\n                              BLACK hours: " );
  187.       gets( inputstr );
  188.       hrs = atoi( inputstr );
  189.       cprintf( "                              BLACK minutes: " );
  190.       gets( inputstr );
  191.       min = atoi( inputstr );
  192.       CountdownTimer t2( hrs, min, BLACK_ );
  193.  
  194.       textcolor( YELLOW );
  195.       cprintf( "\n\n                         Enable flashing clock ticks? " );
  196.       inp = getche();
  197.       if( inp == 'y' || inp == 'Y' )
  198.      t1.visual_ticking_flag = t2.visual_ticking_flag = ON;
  199.       else
  200.      t1.visual_ticking_flag = t2.visual_ticking_flag = OFF;
  201.  
  202.       cprintf( "\n                                                      Enable time warning? " );
  203.       inp = getche();
  204.       if( inp == 'y' || inp == 'Y' )
  205.      {
  206.      t1.time_warning_flag = t2.time_warning_flag = ON;
  207.      cprintf( "                                                          At how many minutes? " );
  208.      gets( inputstr );
  209.      t1.warning = t2.warning = atoi( inputstr );
  210.      }
  211.       else
  212.      t1.time_warning_flag = t2.time_warning_flag = OFF;
  213.  
  214.       textcolor( GREEN | BLINK );
  215.       _setcursortype( _NOCURSOR );
  216.       printf( "\n\n\n\n\n\n\n\n\n\n\n\n" );
  217.       cprintf( "                             PRESS A KEY TO BEGIN" );
  218.       while ( !getch() );
  219.       beep1();  //BEEP
  220.  
  221.       graphics_setup( WHITE );
  222.  
  223.       t1.initialize_clock();
  224.       t1.moves++;
  225.       t1.display_moves();
  226.       t1.clock_on();
  227.       beep1();
  228.  
  229.       t2.initialize_clock();
  230.       t2.clock_on();
  231.       beep2();
  232.  
  233.       while ( PLAY )
  234.      {
  235.      t1.moves++;  
  236.      t1.display_moves();
  237.      t1.clock_on();
  238.       beep1();
  239.      t2.clock_on(); 
  240.       beep2();
  241.      }
  242.  
  243. } // End play()
  244.  
  245.  
  246. void opening_screen()
  247. {
  248.    char topline[] = "Chess Clock",
  249.     by_line[] = "by",
  250.     name_line[] = "M\\Cooper",
  251.     endline[] = "PRESS A KEY";
  252.  
  253.       graphics_setup( CYAN );
  254.       settextstyle( GOTHIC_FONT, HORIZ_DIR, HEADLINE_SIZE );
  255.       settextjustify( CENTER_TEXT, CENTER_TEXT );
  256.       setcolor( MAGENTA );
  257.       outtextxy( TOPX, TOPY, topline );
  258.  
  259.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, BY_LINE_SIZE );
  260.       setcolor( BLUE );
  261.       outtextxy( BY_LINE_X, BY_LINE_Y, by_line );
  262.  
  263.       setfillstyle( BAR_PATTERN, BAR_COLOR );
  264.       bar3d( BAR_LEFT, BAR_TOP, BAR_RIGHT, BAR_BOTTOM, BAR_DEPTH, BAR_TOPFLAG );
  265.  
  266.       setfillstyle( PIE_PATTERN, PIE_COLOR );
  267.       pieslice( PIE1_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  268.       pieslice( PIE2_X, PIE_Y, PIE_STARTANGLE, PIE_ENDANGLE, PIE_RADIUS );
  269.       circle( PIE1_X, PIE_Y, CIRC_RAD );
  270.       circle( PIE2_X, PIE_Y, CIRC_RAD );
  271.       line( LINE1_X, LINE_Y1, LINE1_X, LINE_Y2 );
  272.       line( LINE2_X, LINE_Y1, LINE2_X, LINE2_Y2 );
  273.       setfillstyle( PIE_PATTERN, WHITE );
  274.       bar( B1_LEFT, B1_TOP, B1_RIGHT, B_BOTTOM );
  275.       bar( B2_LEFT, B2_TOP, B2_RIGHT, B_BOTTOM );
  276.  
  277.  
  278.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, NAME_LINE_SIZE );
  279.       setcolor( BLUE );
  280.       outtextxy( NAME_LINE_X, NAME_LINE_Y, name_line );
  281.  
  282.       sleep( DELAY );
  283.  
  284.       settextstyle( TRIPLEX_FONT, HORIZ_DIR, ENDLINE_SIZE );
  285.       setcolor( RED );
  286.       outtextxy( ENDLINE_X, ENDLINE_Y, endline );
  287.  
  288.       getch();
  289.       closegraph();
  290.  
  291.       return;
  292. }
  293.  
  294. void CountdownTimer::ShowFlag()
  295. {
  296.      //Show flag over clock in use.
  297.      setfillstyle( INTERLEAVE_FILL, LIGHTRED );
  298.      bar( XXPOS * p + LXMARGIN, YYPOS,
  299.          XXPOS * p + XWIDTH - LXMARGIN, YYPOS + YHEIGHT );
  300.  
  301.      //Erase flag over clock *not* in use.
  302.      setfillstyle( SOLID_FILL, WHITE );
  303.      if( p == WHITE_ ) pp = BLACK_;
  304.      else pp = WHITE_;
  305.      bar( XXPOS * pp + LXMARGIN, YYPOS,
  306.          XXPOS * pp + XWIDTH - LXMARGIN, YYPOS + YHEIGHT );
  307.  
  308.      return;
  309. }
  310.